﻿
// Browser related code, such as detecting html 5 support and other.
var BrowserApi = new function ()
{
    // The base URL of the application services, this URL must be set manually on deployment or by the deployment script.
    this.ApplicationServicesUrl = "http://localhost:46000"

    // True if the browser supports html 5, otherwise false.
    this.Html5Support = document.createElement('canvas').getContext != undefined
    if (this.Html5Support == true)
    {
        this.Html5Support = (typeof (localStorage) != 'undefined' && typeof (localStorage) != "unknown");
    }

    // The information of the scheduled functions
    this.ScheduleFunctionInfo = []

    // Schedule a function to run after a specific amount of time.
    // functionName: The full name of the function excluding the parameters
    // parameters : The function parameters, if the function is called more than once and did not run yet, 
    //              the old parameters will be replaced by the new parameters.
    // delay: The delay in milliseconds.
    //
    // How does it work:
    // Some browser events like the resize event is fired many times during the resize, this function 
    // will allow you to process only parts of those events and ignore the other that come in between.
    this.ScheduleFunction = function (functionName, parameters, delay)
    {
        var functionInfo = this.ScheduleFunctionInfo[functionName];
        if (!functionInfo)
        {
            functionInfo = this.ScheduleFunctionInfo[functionName] = new Object();
            functionInfo.Executing = false;
        }

        functionInfo.Parameters = parameters
        if (functionInfo.Executing == false)
        {
            var fullFunctionName = 'BrowserApi.ScheduleFunction_Completed( "' + functionName + '" )'
            functionInfo.Executing = true;
            window.setTimeout(fullFunctionName, delay);
        }
    }

    this.ScheduleFunction_Completed = function (functionName)
    {
        var functionInfo = this.ScheduleFunctionInfo[functionName]
        var fullFunctionName = functionName + '(' + functionInfo.Parameters + ')'
        eval(fullFunctionName);
        functionInfo.Executing = false
    }

    // Load an Ecma script or a Json file from the specified path.
    this.LoadScript = function (fileName)
    {
        LoadingFile = true;

        var fileUrl = document.createElement('script')
        fileUrl.setAttribute("type", "text/javascript")
        fileUrl.setAttribute("src", fileName)
        document.getElementsByTagName("head")[0].appendChild(fileUrl)
    }

    this.DisableSelectAndDrag = function (uiElement)
    {
        uiElement.onselectstart = BrowserApi.DisableSelectAndDrag_Internal
        uiElement.ondragstart = BrowserApi.DisableSelectAndDrag_Internal
        uiElement.setAttribute("unselectable", "on")
    }

    this.DisableSelectAndDrag_Internal = function (e)
    {
        return false
    }

    this.FindScrollbarSize = function ()
    {
        var overflow = document.body.style.overflow
        document.body.style.overflow = 'hidden';
        var scrollbarSize = document.body.clientWidth;
        document.body.style.overflow = 'scroll';
        scrollbarSize -= document.body.clientWidth;
        document.body.style.overflow = overflow;

        return scrollbarSize
    }

    // Url Encode a string.
    this.UrlEncode = function (StringToEncode)
    {
        return encodeURIComponent(StringToEncode).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
        replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
    }
}
